package com.huanglj.taobaounion.presenter.implement;

import com.huanglj.taobaounion.model.api.Api;
import com.huanglj.taobaounion.model.domain.CategoriesById;
import com.huanglj.taobaounion.presenter.ICategoriesPagerPresenter;
import com.huanglj.taobaounion.utils.LogUtil;
import com.huanglj.taobaounion.utils.RetrofitManager;
import com.huanglj.taobaounion.utils.UrlUtils;
import com.huanglj.taobaounion.view.IHomePagerCategoryById;

import java.net.HttpURLConnection;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;

public class HomePagerPresenterImpl implements ICategoriesPagerPresenter {

    private static final Integer DEFAULT_PAGE = 1;
    private Map<Integer,Integer> pagesInfo = new HashMap<>();
    private static final String TAG = "HomePagerPresenterImpl";
    private Api mApi;
    private List<CategoriesById.DataBean> mData = new ArrayList<>();
    private Integer mTargetPage;

    @Override
    public void getContentCategoryId(int categoryId) {
        Retrofit retrofit = RetrofitManager.getInstance().getRetrofit();
        mApi = retrofit.create(Api.class);

        Integer targetPage = pagesInfo.get(categoryId);
        if (targetPage == null) {
            targetPage = DEFAULT_PAGE;
            pagesInfo.put(categoryId,targetPage);
        }
        for (IHomePagerCategoryById callBack : callBacks) {
            if (callBack.getCategoryId() == categoryId) {
                callBack.onLoading();
            }
        }
        String url = UrlUtils.CategoriesPagerByIdUrl(categoryId, targetPage);
        Call<CategoriesById> call = mApi.getCategoriesById(url);
        call.enqueue(new Callback<CategoriesById>() {
            @Override
            public void onResponse(Call<CategoriesById> call, Response<CategoriesById> response) {
                if (response.code()== HttpURLConnection.HTTP_OK) {
                    CategoriesById pageContent = response.body();
                    LogUtil.d(TAG,pageContent.getCode()+"   " + pageContent.getMessage());
                    //更新UI
                    mData = pageContent.getData();
                    if (mData.size()>=5){
                        List<CategoriesById.DataBean> looperData = mData.subList(mData.size() - 5, mData.size());
                        handleLooperResult(looperData, categoryId);
                    }else {
                        handleLooperResult(mData,categoryId);
                    }

                    handleHomePagerResult(mData,categoryId);
                }
            }

            @Override
            public void onFailure(Call<CategoriesById> call, Throwable t) {
                if (callBacks != null) {
                    for (IHomePagerCategoryById callBack : callBacks) {
                        if (callBack.getCategoryId() == categoryId) {
                            callBack.onError(t.toString());
                            LogUtil.e(TAG,t.toString());
                        }
                    }
                }
            }
        });
    }

    private void handleHomePagerMoreResult(List<CategoriesById.DataBean> moreData, int categoryId) {
        for (IHomePagerCategoryById callBack : callBacks) {
            if (categoryId == callBack.getCategoryId()) {
                if (moreData.size()==0) {

                    callBack.onLoadMoreEmpty();
                }else {
                    callBack.onLoadMoreLoad(moreData);
                }
            }

        }
    }

    private void handleLooperResult(List<CategoriesById.DataBean> data, int categoryId) {
        for (IHomePagerCategoryById callBack : callBacks) {
            if (callBack.getCategoryId()==categoryId) {
                if (data == null||data.size()==0) {
                    callBack.onLoopLoad(null);
                }else {
                    callBack.onLoopLoad(data);
                }
            }


        }
    }

    private void handleHomePagerResult(List<CategoriesById.DataBean> data,int categoryId) {

        for (IHomePagerCategoryById callBack : callBacks) {
            if (callBack.getCategoryId() == categoryId) {
                if (data == null || data.size()==0){
                    callBack.onEmpty();

                }else {
                    LogUtil.d(TAG,"get data size :" + data.size());
                    callBack.onContentLoaded(data);
                }
            }

        }
    }

    private List<IHomePagerCategoryById> callBacks = new ArrayList<>();


    @Override
    public void getContentCategoryTitle(String title) {

    }

    @Override
    public void loadMore(int categoryId) {
        mTargetPage = pagesInfo.get(categoryId);
        if (mTargetPage == null) {
            mTargetPage = DEFAULT_PAGE;
            pagesInfo.put(categoryId, mTargetPage);
        }else {
            mTargetPage++;
            pagesInfo.put(categoryId, mTargetPage);
        }
        String url = UrlUtils.CategoriesPagerByIdUrl(categoryId, mTargetPage);
        Call<CategoriesById> call = mApi.getCategoriesById(url);
        call.enqueue(new Callback<CategoriesById>() {
            @Override
            public void onResponse(Call<CategoriesById> call, Response<CategoriesById> response) {
                List<CategoriesById.DataBean> moreData = response.body().getData();


                handleHomePagerMoreResult(moreData,categoryId);
            }

            @Override
            public void onFailure(Call<CategoriesById> call, Throwable t) {
                mTargetPage--;
                pagesInfo.put(categoryId,mTargetPage);
                for (IHomePagerCategoryById callBack : callBacks) {
                    callBack.onLoadMoreError();
                }
            }
        });
    }

    @Override
    public void reLoad(int categoryId) {

    }

    @Override
    public void registerViewCallBack(IHomePagerCategoryById callBack) {
        if (!callBacks.contains(callBack)) {
            callBacks.add(callBack);
        }
    }

    @Override
    public void unregisterViewCallBack(IHomePagerCategoryById callBack) {
        callBacks.remove(callBack);
    }
}
